// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
var Root = require("Root");
cc.Class({
    extends: cc.Component,

    properties: {

    },



    onLoad() {
        cc.localImage = false;           //true用本地图片测试, false用网络图片
    },

    start() {
        //2019-1-7 读取网络图片列表 新加功能
        //浏览器报错跨域，模拟器和手机运行良好，图片测试通过，txt文件读取有问题
        if (!cc.localImage) {
            // var url = "http://106.12.147.168:8000/AmazingPuzzleImage/AfcriaArchitecture/1.png";
            // cc.loader.load(url, function (err, data) {
            //     if (err) {
            //         console.log("失败");
            //         cc.log(err);
            //     }
            //     else {
            //         console.log("成功")
            //         console.log(data);
            //         var newNode = new cc.Node("test");
            //         newNode.parent = cc.find("Canvas");
            //         newNode.addComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(data);

            //     }
            // }.bind(this));
            //var texturl = "http://106.12.147.168:8000/AmazingPuzzleImage/ImageMenu.json";       // 百度云测试服务器，较慢
            var texturl = "http://crazyfind-static.boomegg.cn/PuzzleImage/ImageMenu.json";      //爆弹测试服务器
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                    var response = xhr.responseText;
                    console.log(response);
                    console.log("解析");
                    cc.ImageMenu = JSON.parse(response.toString());

                    console.log("存储");
                    console.log(Object.keys(cc.ImageMenu).length);
                }
            };
            xhr.open("GET", texturl, true);
            xhr.send();
        }
        else {
            //本地测试数据
            cc.ImageMenu = {
                "AfcriaArchitecture": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "AfcriaCulture": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "AfricaNationalTreasure": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "AfricaPlantsAndAnimals": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "AmericaArchitecture": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "AmericaCulture": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "AmericaNationalTreasure": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "AmericaPlantsAndAnimals": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "AsiaArchitecture": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "AsiaCulture": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "AsiaNationalTreasure": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "AsiaPlantsAndAnimals": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "EuropeArchitecture": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "EuropeCulture": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "EuropeNationalTreasure": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "EuropePlantsAndAnimals": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "OceaniaArchitecture": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "OceaniaCulture": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "OceaniaNationalTreasure": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "OceaniaPlantsAndAnimals": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "Crocodile": [[8, 6], [8, 6], [8, 6], [8, 6], [8, 6], [8, 6]],
                "others": [[8, 6], [8, 6]],
                "SmallRedCap": [[8, 6], [8, 6], [8, 6]],
                "Terror": [[8, 6], [8, 6], [8, 6], [8, 6]]
            };
        }
        //2019-1-8 读取碎片模板 新加功能
        cc.loader.loadResDir("BasePiece/48", cc.SpriteFrame, function (err, pieces) {
            if (err) {
                console.log("失败");
                cc.log(err);
            }
            else {
                console.log("小片模板成功48");
                cc.piece48 = [];
                for (let index = 0; index < pieces.length; index++) {
                    let texture = pieces[index].getTexture();
                    texture.name = pieces[index].name;
                    cc.piece48[pieces[index].name] = texture;
                }
                // for (let index = 1; index <= pieces.length; index++) {
                //     console.log(cc.piece48[index]);
                // }
            }
        }.bind(this));
        cc.loader.loadResDir("BasePiece/24", cc.SpriteFrame, function (err, pieces) {
            if (err) {
                console.log("失败");
                cc.log(err);
            }
            else {
                console.log("小片模板成功24");
                cc.piece24 = [];
                for (let index = 0; index < pieces.length; index++) {
                    let texture = pieces[index].getTexture();
                    texture.name = pieces[index].name;
                    cc.piece24[pieces[index].name] = texture;
                }
                // for (let index = 1; index <= pieces.length; index++) {
                //     console.log(cc.piece24[index]);
                // }
            }
        }.bind(this));

    },
    StartGame() {
        var index = cc.find("Canvas/ChoosePageView").getComponent(cc.PageView).getCurrentPageIndex();
        var root = cc.find("root").getComponent(Root);
        root.currentPageIndex = index;

        if (index == 0) {
            console.log(cc.find("Canvas/ChooseNumToggleContainer").getChildByName("toggle1").getComponent(cc.Toggle).isChecked);
            if (cc.find("Canvas/ChooseNumToggleContainer").getChildByName("toggle1").getComponent(cc.Toggle).isChecked == true) {
                console.log("24");
                root.kindPuzzle = "Crocodile_24";
                root.row = 6;
                root.column = 4;


            }
            else {
                console.log("48");
                root.kindPuzzle = "Crocodile";
                root.row = 8;
                root.column = 6;

            }
            root.index = 1;

        }
        else if (index == 1) {
            root.kindPuzzle = "Others";
            root.index = 1;
            root.row = 8;
            root.column = 6;

        }
        else if (index == 2) {
            root.kindPuzzle = "SmallRedCap";
            root.index = 1;
            root.row = 8;
            root.column = 6;

        }
        else if (index == 3) {
            root.kindPuzzle = "Terror";
            root.index = 1;
            root.row = 8;
            root.column = 6;

        }
        else if (index == 4) {          //2019-1-8 随机选择关卡，随机选择网络图片  测试功能
            console.log("随机模式");
            var allKind = Object.keys(cc.ImageMenu);
            console.log(allKind);
            var kindIndex = Math.floor(Math.random() * allKind.length);
            var thisKindName = allKind[kindIndex];
            console.log(thisKindName);

            var thisKind = cc.ImageMenu[thisKindName];
            var levelIndex = Math.floor(Math.random() * thisKind.length);
            var thisLevel = thisKind[levelIndex];
            console.log(thisLevel);
            root.kindPuzzle = thisKindName;
            root.index = levelIndex;
            root.row = thisLevel[0];
            root.column = thisLevel[1];
            root.net = true;
            console.log("加载网络图片");
        }
        console.log(cc.find("Canvas/ChooseRotateToggleContainer").getChildByName("toggle1").getComponent(cc.Toggle).isChecked);
        if (cc.find("Canvas/ChooseRotateToggleContainer").getChildByName("toggle1").getComponent(cc.Toggle).isChecked == true) {
            root.isRotate = true;
        }
        else {
            root.isRotate = false;
        }
        cc.director.loadScene("puzzle");
    },
    StartGame2() {
        cc.find("Canvas/ShelfPuzzleInfo").active=false;
        var root = cc.find("root").getComponent(Root);
        console.log("随机模式");
        var allKind = Object.keys(cc.ImageMenu);
        console.log(allKind);
        var kindIndex = Math.floor(Math.random() * allKind.length);
        var thisKindName = allKind[kindIndex];
        console.log(thisKindName);

        var thisKind = cc.ImageMenu[thisKindName];
        var levelIndex = Math.floor(Math.random() * thisKind.length);
        var thisLevel = thisKind[levelIndex];
        console.log(thisLevel);
        root.kindPuzzle = thisKindName;
        root.index = levelIndex;
        root.row = thisLevel[0];
        root.column = thisLevel[1];
        root.net = true;
        console.log("加载网络图片");
      
        if (cc.find("Canvas/ShelfPuzzleInfo/New ToggleContainer").getChildByName("toggle1").getComponent(cc.Toggle).isChecked == true) {
            root.isRotate = true;
        }
        else {
            root.isRotate = false;
        }
      
        cc.director.loadScene("puzzle");
    },
    // update (dt) {},
});
